﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.ApplicationPages.Calendar.RecurrenceRule;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindStyle.SPCalendar
{
    public class Event<T> : Event
        where T : Pattern.IPattern, new()
    {
        public Recurrence<T> Recurrence { get; set; }

        public Event()
        {
            this.Start = DateTime.Now.AddMinutes(30);
            if (this.Start.Minute > 30)
                this.Start.AddMinutes(30 - this.Start.Minute);
            else
                this.Start.AddMinutes(0 - this.Start.Minute);

            this.End = this.Start.AddHours(1);

            this.Recurrence = new Recurrence<T>();
        }

        public override void Read(SPListItem item, SPRegionalSettings regionalSettings)
        {
            base.Read(item, regionalSettings);

            //TODO: support attachment in future
            //if (item.Attachments.Count > 0)
            //{
            //    this.HasFile = true;
            //    this.FileName = string.Format("{0}/{1}/attachments/{2}/{3}",
            //        item.Web.Url,
            //        item.ParentList.RootFolder.Url,
            //        item.ID,
            //        item.Attachments[0]);
            //}

            this.Recurrence.Read(item, regionalSettings);
        }

        public override void Write(SPListItem item, SPRegionalSettings regionalSettings = null)
        {
            this.Start = this.Start.AddSeconds(-this.Start.Second);
            this.End = this.End.AddSeconds(-this.End.Second);

            base.Write(item, regionalSettings);

            item[SPBuiltInFieldId.fRecurrence] = false;
            item[SPBuiltInFieldId.RecurrenceData] = null;
            item[SPBuiltInFieldId.EventType] = 0;
            item[SPBuiltInFieldId.UID] = null;
            item[SPBuiltInFieldId.TimeZone] = null;

            //TODO: support attachment in future
            //if (!evt.HasFile)
            //{
            //    for (int i = item.Attachments.Count - 1; i >= 0; i--)
            //    {
            //        item.Attachments.Delete(item.Attachments[i]);
            //    }
            //}
            //else if (!string.IsNullOrEmpty(evt.FileName)
            //        && evt.File != null
            //        && evt.File.Length > 0)
            //{
            //    for (int i = item.Attachments.Count - 1; i >= 0; i--)
            //    {
            //        item.Attachments.Delete(item.Attachments[i]);
            //    }
            //    item.Attachments.Add(evt.FileName, evt.File);
            //}

            if (this.IsRecurrence)
            {
                item[SPBuiltInFieldId.WorkspaceLink] = false;
                item[SPBuiltInFieldId.Duration] = (int)(this.End.TimeOfDay - this.Start.TimeOfDay).TotalMinutes;
                item[SPBuiltInFieldId.fRecurrence] = -1;
                item[SPBuiltInFieldId.RecurrenceData] = this.Recurrence.ToXML(regionalSettings);
                item[SPBuiltInFieldId.EventType] = 1;
                item[SPBuiltInFieldId.UID] = System.Guid.NewGuid();
                item[SPBuiltInFieldId.TimeZone] = regionalSettings.TimeZone.ID;

                var info = regionalSettings.TimeZone.Information;
                item[SPBuiltInFieldId.XMLTZone] = string.Format(@"
<timeZoneRule>
<standardBias>{0}</standardBias>
<additionalDaylightBias>{1}</additionalDaylightBias>
</timeZoneRule>",
                    info.Bias,
                    info.DaylightBias).Replace("\r\n", string.Empty);

                var start = this.Recurrence.DateRange.StartDate.Date + this.Start.TimeOfDay;
                item[SPBuiltInFieldId.StartDate] = start;

                DateTime end;
                if (this.Recurrence.DateRange.End.Type == EndType.ByDate)
                    end = this.Recurrence.DateRange.End.Date.Date + this.End.TimeOfDay;
                else
                    end = start.AddYears(100);
                item[SPBuiltInFieldId.EndDate] = end;
            }
        }
    }
}
